#include "at24cxx.h"
#include "iic.h"

void at24cxxWdata(u16 addr,u8 data)
{
	iicStart();
	iicSendByte(0xa0);
	if(iicWaitACK()!=ACK)
	{
		iicStop();
		return;
	}
	
	iicSendByte(addr);
	if(iicWaitACK() != ACK)
	{
		iicStop();
		return;
	}
	iicSendByte(data);
	if(iicWaitACK() != ACK)
	{
		iicStop();
		return;
	}
	iicStop();
}

u8 at24cxxRdata(u16 addr)
{
	u8 read_data = 0;
	iicStart();
	iicSendByte(0xa0);
	if(iicWaitACK() != ACK)
	{
		iicStop();
		return 0;
	}
	
	iicSendByte(addr);
	if(iicWaitACK() != ACK)
	{
		iicStop();
		return 0;
	}

	iicStart();
	iicSendByte(0xa1);
	if(iicWaitACK() != ACK)
	{
		iicStop();
		return 0;
	}
	read_data = iicReceiveByte(NACK);
	iicStop();
	
	return read_data;
}
